package com.hy.heath.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hy.heath.common.BusinessException;
import com.hy.heath.mapper.VaccineRecordMapper;
import com.hy.heath.pojo.entity.VaccineRecord;
import com.hy.heath.service.UserService;
import com.hy.heath.service.VaccineRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.time.LocalDate;
import java.util.List;

//疫苗记录
@Service
public class VaccineRecordServiceImpl extends ServiceImpl<VaccineRecordMapper, VaccineRecord> implements VaccineRecordService {

    @Autowired
    private UserService userService;

    @Override
    public List<VaccineRecord> listByUserId(Integer userId) {
        // 校验用户存在性
        if (userService.getById(userId) == null) {
            throw new BusinessException("用户不存在");
        }

        // 根据用户ID查询疫苗记录，并按接种日期降序排列
        return this.list(new LambdaQueryWrapper<VaccineRecord>()
                .eq(VaccineRecord::getUserId, userId)
                .orderByDesc(VaccineRecord::getVaccinationDate));
    }

    @Override
    public Page<VaccineRecord> pageRecords(Page<VaccineRecord> page, Integer userId) {
        LambdaQueryWrapper<VaccineRecord> queryWrapper = new LambdaQueryWrapper<>();
        if (userId != null) {
            queryWrapper.eq(VaccineRecord::getUserId, userId);
        }
        queryWrapper.orderByDesc(VaccineRecord::getVaccinationDate);
        return this.page(page, queryWrapper);
    }

    @Override
    public boolean save(VaccineRecord entity) {
        // 新增校验逻辑
        validateVaccineRecord(entity);
        return super.save(entity);
    }

    @Override
    public boolean updateById(VaccineRecord entity) {
        // 更新校验逻辑
        validateVaccineRecord(entity);
        return super.updateById(entity);
    }

// 验证疫苗记录
    private void validateVaccineRecord(VaccineRecord record) {
        // 如果用户ID为空，抛出异常
        if (record.getUserId() == null) {
            throw new BusinessException("用户ID不能为空");
        }
        // 如果疫苗名称为空，抛出异常
        if (!StringUtils.hasText(record.getVaccineName())) {
            throw new BusinessException("疫苗名称不能为空");
        }
        // 如果接种日期为空，抛出异常
        if (record.getVaccinationDate() == null) {
            throw new BusinessException("接种日期不能为空");
        }
    }
    // 在Service中添加
    // 获取即将过期的疫苗记录
    public List<VaccineRecord> getExpiringRecords(LocalDate checkDate) {
        // 使用LambdaQueryWrapper查询即将过期的疫苗记录
        return this.list(new LambdaQueryWrapper<VaccineRecord>()
                // 查询nextVaccinationDate不为空的记录
                .isNotNull(VaccineRecord::getNextVaccinationDate)
                // 查询nextVaccinationDate在checkDate之后的记录
                .le(VaccineRecord::getNextVaccinationDate, checkDate.plusDays(30))
                // 按照nextVaccinationDate升序排列
                .orderByAsc(VaccineRecord::getNextVaccinationDate));
    }

}
